package cn.bonoon.controllers.announcement;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bonoon.core.plugins.AnnouncementService;
import cn.bonoon.entities.interacting.AnnouncementEntity;
import cn.bonoon.kernel.web.ButtonEventType;
import cn.bonoon.kernel.web.ButtonRefreshType;
import cn.bonoon.kernel.web.annotations.grid.GridStandardDefinition;
import cn.bonoon.kernel.web.controllers.AbstractGridController;
import cn.bonoon.kernel.web.models.JsonResult;

@Controller
@RequestMapping("/s/sys/announcement")
public class AnnouncementController extends AbstractGridController<AnnouncementEntity, AnnouncementItem> {
	private final AnnouncementService service;

	@Autowired
	public AnnouncementController(AnnouncementService service) {
		super(service);
		this.service = service;
	}

	@Override
	protected Class<AnnouncementItem> itemClass() {
		return AnnouncementItem.class;
	}

	@Override
	@GridStandardDefinition(
			insertClass = AnnouncementEditor.class, 
			updateClass = AnnouncementEditor.class, 
			detailClass = AnnouncementDetail.class)
	protected AnnouncementService initLayoutGrid(LayoutGridRegister register) throws Exception {
		// 添加按键
		register.button("发布/取消", "changeStatus.do", ButtonEventType.GET,ButtonRefreshType.FINISH);
//		DialogInsertHandler.toolbar(register, service, AnnouncementEditor.class);
//		OperateIdsHandler.delete(register, service);
//
//		DialogDetailHandler.button(register, service, AnnouncementDetail.class);
//		DialogUpdateHandler.button(register, service, AnnouncementEditor.class);
//		OperateIdHandler.delete(register, service);
		
		return service;
	}

	@ResponseBody
	@RequestMapping(value = "!{key}/changeStatus.do", method = { POST, GET })
	public Object changeReleaseStatus(HttpServletRequest request, 
			@PathVariable("key") String key, 
			@RequestParam("id") Long id) {
		try{
			service.changeReleaseStatus(getUser(), id);
			return JsonResult.result();
		}catch (Exception e) {
			return JsonResult.error(e);
		}
	}
}
